<?php
namespace Home\Common;
use Think\Controller;
use Think\Model;
class GetDetail extends \Thread {
    protected $subUrl;
    public function __construct($subUrl){
        $this->subUrl = $subUrl;
    }
    public function run(){
		$this->getUserDetail($this->subUrl);
    }
    /**
     *获取一个分类下的所有分页信息
     */
    private function getUserDetail($subUrl){
        
        for($page=1;$page<=100;$page++){
            $subUrls=$subUrl."pn$page/";
            $ch = curl_init($subUrls); //初始化会话
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_COOKIE, $this->config_arr['user_cookie']);  //设置请求COOKIE
            //curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
            $result = curl_exec($ch);
            if(strpos($result,'infoList')){
                $pattern='/<ul class=\"infoList\">.*<\/ul>/isU';
            }
            else if(strpos($result,'list-info')){
                $pattern='/<ul class=\"list-info\">.*<\/ul>/isU';
            }
            else {
                return;
            }
            preg_match($pattern,$result,$match);
            $pattern1='/<a href=\".*\"/isU';
            preg_match_all($pattern1,$match[0],$match);
            foreach ($match[0] as $key => $value) {
                if(strlen($value)>400)continue;
                $value=substr($value,strpos($value,'http'));
                $itemUrl[]=substr($value,0,strlen($value)-1);
            }
            $this->analysisDetail($itemUrl);
            curl_close($ch);
            //return;
        }
    }
    /*
     *解析数据，写入数据库
     */
    private function analysisDetail($detailUrl){
        $model=M('contact');
        foreach ($detailUrl as $key => $value) {
            $ch = curl_init($value); //初始化会话
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_COOKIE, $this->config_arr['user_cookie']);  //设置请求COOKIE
            //curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
            $result = curl_exec($ch);
            $h1Preg = '/<h1 class="tit">(.*?)<\/h1>/is';
            preg_match($h1Preg,$result,$h1str);
            $title=$h1str[1];
            $pattern='/<article class=\"article\">(.*)<\/article>/is';
            preg_match($pattern,$result,$body);
            $patternli='/<li>(.*?)<\/li>/is';
            preg_match_all($patternli,$body[1],$li);
            $places="";
            $li=$li[1];
            $chinesePreg='/[\x{4e00}-\x{9fa5}]+/u';
            if(count($li)==5){
                preg_match_all($chinesePreg,$li[0],$type);
                preg_match_all($chinesePreg,$li[1],$place);
                preg_match_all($chinesePreg,$li[2],$name);
                preg_match('/1[34578]{1}\d{9}/',$li[3],$phone);
                foreach ($place[0] as $key => $values) {
                    if($key===0)continue;
                    $places.=$values;
                }
            }
            else{
                preg_match_all($chinesePreg,$li[0],$type);
                preg_match_all($chinesePreg,$li[1],$name);
                preg_match('/1[34578]{1}\d{9}/',$li[2],$phone);
            }
            if(empty($phone[0])||empty($name[0][3])){
                continue;
            }
            echo $value.'<br>';
            echo $title.'<br>';
            echo $name[0][3].'<br>';
            echo $phone[0].'<br>';
            echo $type[0][1].'<br>';
            echo $places.'<br>';
            $data['title']=$title;
            $data['name']=$name[0][3];
            $data['phone_num']=$phone[0];
            $data['address']=$places;
            $data['type']=$type[0][1];
            if($model->Insert($data)){
                echo 'true<br>';
            }
            echo '<hr>';
            flush();
            usleep(10);
        }
    }
}